<?php
/**
 * 现金卡 ajax类
 * @package  ajax
 * @subpackage  company
 * @author   sunqiang
 */

quanli_require('module/base/company/cardType.class.php');

class ajax_cardType{

    public function execute() {

        if(isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])){

            switch($_REQUEST['operFlg']){

                case 1://代金卡类型列表
                    $this->cardTypeList();
                    break;

                case 2://代金卡类型列表
                    $this->saveCardType();
                    break;

                default:

                    returnJsonData('参数错误');

                    break;
            }

        }else {
            returnJsonData('参数错误');
        }
    }


    private function saveCardType()
    {
        global $G_PGLOB;

        $card_type = new entity('card_type');
        $card_type->creator = $G_PGLOB->currentUserName;
        $card_type->card_type_name = $_REQUEST['card_type'] ;
        $G_PGLOB->DBCA->save($card_type);

        if($card_type)
        {
            returnJsonData("成功新增代金卡类型",200);
        }
        returnJsonData("新增代金卡类型失败!!");
    }

    private function cardTypeList()
    {
        global $G_PGLOB;
        $arg_array = $_REQUEST;

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        //实例化现金卡类
        $cardType = new cardType();
        $cardType_array = $cardType->search( $arg_array );

        $result_array['total'] = $arg_array['count'];
        $result_array['page'] = $arg_array['page'];
        foreach($cardType_array as $type)
        {
            $result_array['rows'][] = array(
                'id' => $type['card_type_id'],
                'cell' => array(
                    '',
                    $type['card_type_id'],
                    $type['card_type_name']
                )
            );
        }


        echo json_encode($result_array);
    }

    /**
     * 现金卡列表
     */
    private function cashCardList(){

        global $G_PGLOB;

        //实例化现金卡类
        $cardFaceValue = new cardFaceValue();
        $arg_array = $_REQUEST;
        $result = $cardFaceValue->search( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($result) {

            $edit_permission = $G_PGLOB->has_permission(70003);

            foreach($result as &$data) {

                if ($edit_permission) {



                    $operation_html = '';
                    $operation_html .= '<a href="javascript:void(0);" onclick="cardList.edit.open(' . $data['face_value_id'] . ');">修改</a>&nbsp;';

                    $result_array['rows'][] = array (
                        'id' => $data['face_value_id'],
                        'cell' => array(
                            $operation_html,
                            $data['face_value_id'],
                            $data['face_value'],
                            $data['card_info'],

                        )
                    );

                } else {

                    $operation_html = '';
                    $result_array['rows'][] = array (
                        'id' => $data['face_value_id'],
                        'cell' => array(
                            $operation_html,
                            $data['face_value_id'],
                            $data['face_value'],
                            $data['card_info'],

                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }



}
